package com.simon.app.october.modules.sys.service.impl;

import com.simon.app.october.modules.sys.dao.SysRoleMenuDao;
import com.simon.app.october.modules.sys.entity.SysRoleMenuEntity;
import com.simon.app.october.modules.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    SysRoleMenuDao roleMenuDao;

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        return roleMenuDao.getMenuIdsByRoleId(roleId);
    }

    @Override
    public void saveOrUpdate(Long roleId, List<Long> menuIds) {
        // 删除roleId关联的menuIds
        roleMenuDao.deleteByRoleIds(Arrays.asList(roleId));

        // 更新
        if (0 == menuIds.size()) {
            return ;
        }

        List<SysRoleMenuEntity> list = new ArrayList<>(menuIds.size());

        for (Long menuId : menuIds) {
            SysRoleMenuEntity roleMenuEntity = new SysRoleMenuEntity();
            roleMenuEntity.setRoleId(roleId);
            roleMenuEntity.setMenuId(menuId);

            list.add(roleMenuEntity);
        }

        roleMenuDao.save(list);
    }

    @Override
    public void deleteByRoleIds(Long[] roleIds) {
        roleMenuDao.deleteByRoleIds(Arrays.asList(roleIds));
    }
}
